import axios from 'axios'
import { useCounterStore } from '@/stores/counter'
import router from '@/router'
import { ElMessage } from 'element-plus'



//创建axios实例
const request = axios.create({
    baseURL: import.meta.env.VITE_APP_BASE_API,
    timeout: 5000
})

//请求拦截器
request.interceptors.request.use((config) => {
    const store = useCounterStore()
    config.headers['token'] = store.user.token
    console.log(store.user);
    return config;
})
//响应拦截器
request.interceptors.response.use((response) => {
    return response.data;
}, (error) => {
    if (error.response.status === 401) {
        ElMessage.error('token无效')
        //跳转登录页
        router.push('/login')
    }
    return Promise.reject(error);
})

//路由守卫


export default request